/*
 * File Name: RemoteApplicationList.java
 * Created on: 
 */

package co.edu.uniandes.intercon.core.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;

import co.edu.uniandes.intercon.server.util.ConnectionHelper;

/**
 * <code>Singleton</code> used to store the applications shared remotely
 * 
 * @author Fabian Gutierrez [fagossa@gmail.com]
 */
public class RemoteApplicationList extends Observable {

	/*
	 * Singleton instance
	 */
	private static RemoteApplicationList instance;

	/*
	 * application list
	 */
	private List<BaseDto> applications;

	// avoids instantiation outside the class
	private RemoteApplicationList() {
		applications = new ArrayList<BaseDto>();
	}

	/** Creates a new instance of Application list */
	public static synchronized RemoteApplicationList getInstance() {
		if (instance == null) {
			instance = new RemoteApplicationList();
		}
		return instance;
	}

	/**
	 * Retrieves current application list
	 * 
	 * @return application list
	 */
	public List<BaseDto> getApplicationList() {
		return applications;
	}

	/**
	 * Set the current message and notity all the observers of this class about
	 * the change
	 * 
	 * @param applicationDto
	 *            Application to add to the list
	 */
	public void addMessage(BaseDto applicationDto) {
		applications.add(applicationDto);
		super.setChanged();
		super.notifyObservers();
	}

	/**
	 * Removes an application from the list
	 * 
	 * @param originalDto
	 *            Application to be removed from the system
	 */
	public void remove(ApplicationDto originalDto) {
		boolean changed = false;
		for (BaseDto baseDto : applications) {
			ApplicationDto baseAppDto = (ApplicationDto) baseDto;
			if (areApplicationsTheSame(baseAppDto, originalDto)) {
				applications.remove(baseDto);
				changed = true;
				break;
			}
		}
		if (changed) {
			super.setChanged();
			super.notifyObservers();
		}
	}
	
	/**
	 * Clear the element list
	 */
	public void clear() {
		applications.clear();
		super.setChanged();
		super.notifyObservers();
	}

	/**
	 * Verify if an application already exist in the list
	 * @param applicationDto	Application to be searched
	 * @return	<code>true</code> if exist or <code>false</code> otherwise
	 */
	public boolean exist(ApplicationDto applicationDto) {
		boolean exist = false;
		for (BaseDto baseDto : applications) {
			ApplicationDto baseAppDto = (ApplicationDto) baseDto;
			if (areApplicationsTheSame(baseAppDto, applicationDto)) {
				exist = true;
				break;
			}
		}
		return exist;
	}

	/*
	 * Verify if two application are equals
	 */
	private boolean areApplicationsTheSame(ApplicationDto baseAppDto,
			ApplicationDto originalDto) {
		/*
		boolean complement = true; // TODO: verify file length!!
		String currHostAddress = ConnectionHelper.getCurrentHost();
		return (baseAppDto.getHost().equals(currHostAddress)) // same host
				&& (baseAppDto.getPort() == originalDto.getPort()) // same port
				&& (baseAppDto.getName().equals(originalDto.getName()) && (baseAppDto
						.getType().equals(originalDto.getType()))); // same
		// name
		 * 
		 */
		return baseAppDto.getId()==originalDto.getId();
	}
	
}
